/**
 * 系统工具类模块
 * 1、自定义ID
 * 2、get/post
 */
define(["jquery", "juicer", "humane"], function($, j, humane) {
	window.hh=humane;
	var tplIcon = j(document.getElementById("tpl-icon").innerHTML);

	var _self = {
		
		map2Obj:function(map){
			var o={};
			map.forEach(function(val,key,_map){
				o[key]=val
			})
			return o;
		},

		createIcon: function(data) {
			var h = $(tplIcon.render(data));
			
			$("body").append(h);
			return h;
		},

		toast: function(text, type) {
			type = type || "success";
			var h = humane.create({
				timeout: 4000,
				clickToClose: false,
				addnCls: "humane-flatty-" + type
			})
			h.log(text);
		},

		get: function(url, success, error) {
			$.ajax({
				url: url,
				type: "get",
				success: function(res) {
					success && success(res);
				},
				error: function() {
					error && error();
				}
			})
		},

		post: function(url, data, success, error) {
			$.ajax({
				type: "post",
				url: url,
				data: JSON.stringify(data),
				contentType: "application/json;charset=utf-8",
				success: function(res) {
					success && success(res);
				},
				error: function() {
					error && error();
				}
			})
		},

		dateFormat: function(data, format) {
			if (!(data instanceof Date)) {
				data = new Date(Number(data));
			}
			format = format || "y-M-d h:m:s";
			var f = ["y", "M", "d", "h", "m", "s"];
			var array = [data.getFullYear(), data.getMonth() + 1, data.getDate(),
				data.getHours(), data.getMinutes(), data.getSeconds()
			];
			for (var i = 0; i < 6; i++) {
				var _t = array[i] < 10 ? "0" + array[i] : array[i];
				format = format.replace(f[i], _t);
			}
			return format;
		},

		timeFormat: function(val) {
			if (val) {
				return _self.dateFormat(val);
			}
			return "";
		},

		sessionStatus: function(val) {
			if (val == 0) {
				return "<font color='#FF0000'>排队中...</font>";
			} else if (val == 1) {
				return "<font color='#F1C40F'>正在处理...</font>";
			} else if (val == 2) {
				return "<font color='#2ECC71'>已完成</font>";
			}
		},
		levelStatus: function(val) {
			if (val == 1) {
				return "<font color='#FF0000'>紧急</font>";
			} else if (val == 2) {
				return "<font color='#FF6600'>高</font>";
			} else if (val == 3) {
				return "<font color='#ffcc00'>中</font>";
			} else if (val == 4) {
				return "<font color='#ccff00'>低</font>";
			}
		},
		agentStatus: function(val) {
			if (val == 0) {
				return "<font color='#CFCFCF'>离线状态</font>";
			} else if (val == 1) {
				return "<font color='#339933'>激活状态</font>";
			} else if (val == 2) {
				return "<font color='#FF0000'>负荷状态</font>";
			}else if (val == 3) {
				return "<font color='#ffa000'>休眠状态</font>";
			}else if (val == 4) {
				return "<font color='#000000'>退出登录</font>";
			}
		},
		
		ticketStatus: function(val) {
			if (val == 0) {
				return "<font color='#FF0000'>等待处理</font>";
			} else if (val == 1) {
				return "<font color='#60C1CE'>正在处理</font>";
			} else if (val == 2) {
				return "<font color='#FF80AB'>持续跟进</font>";
			}else if (val == 3) {
				return "<font color='#FFC444'>工单驳回</font>";
			}else if (val == 4) {
				return "<font color='#339933'>处理完毕</font>";
			}
		},
		
		down:function(_this){
			if($(_this).html()!="安装"){
				_self.toast("请稍等...");
				return ;
			}
			$(_this).html("正在安装...");
			var name=_this.getAttribute("name");
			_self.post("/sys/ol/i/plu",{
				path:name
			},function(res){
				if(res.code==0){
					_self.toast("安装完成");
					$(_this).replaceWith("安装完成");
				}else{
					_self.toast("安装失败","error");
					$(_this).replaceWith("安装失败");
				}
			},function(){
				d.replaceWith("安装失败");
			})
			
		}

	}

	return _self;

})